<template>
  <div id="login-page">
    <div class="login-box">
      <!-- 头像区域 -->
      <div class="avator">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 表单区域 -->
      <!-- 添加ref属性vue提供，获取dom对象或者组件实例 -->
      <el-form
        class="login-form"
        :model="loginForm"
        :rules="loginFormRules"
        ref="loginForm"
      >
        <el-form-item prop="username">
          <el-input
            prefix-icon="iconfont icon-user"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-3702mima"
            v-model="loginForm.password"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },

  created() {},

  methods: {
    resetLoginForm() {
      // resetFields为element库中form组件的方法
      this.$refs.loginForm.resetFields()
    },
    login() {
      this.$refs.loginForm.validate(async flag => {
        if (!flag) return
        const res = await this.$axios.post('/login', this.loginForm)
        // const res = await this.$axios.post('/login', this.loginForm)
        if (res.meta.status !== 200) {
          return this.$message.error({
            message: '登陆失败',
            showClose: true
          })
        }
        this.$message.success('登陆成功')
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style scoped lang='less'>
#login-page {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
  display: flex;
  justify-content: center;
  align-items: center;
  .login-box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 5px;
    .avator {
      width: 100px;
      height: 100px;
      border-radius: 50%;
      box-shadow: 0px 0px 10px green;
      overflow: hidden;
      margin: -50px auto 0;
      background-color: #fff;
      display: flex;
      justify-content: center;
      align-items: center;
      img {
        width: 70%;
        height: 70%;
        margin-top: 10px;
      }
    }
    .login-form {
      margin-top: 30px;
      padding: 0 15px;
      .btns {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
